package com.eminem.controller.article;

import com.eminem.common.StrMath;
import com.eminem.controller.BaseServlet;
import com.eminem.model.Article;
import com.eminem.model.Favorite;
import com.eminem.service.ArticleService;
import com.eminem.service.impl.ArticleServletimpl;
import com.eminem.utils.PageUtil;



import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;

@WebServlet("/article/*")
public class ArticleController extends BaseServlet {

    private ArticleService articleService=new ArticleServletimpl();

    public void lookRelease(HttpServletRequest req, HttpServletResponse resp){
        try {
            String page = req.getParameter("page");
            String name = req.getParameter("name");
            String userId = getUserId(req);
//            System.out.println(name);
            int currentPage = 1;
            if (null != page){
                currentPage = Integer.parseInt(page);
            }
            List<Map<String, Object>> list = articleService.getArticleAll(userId,currentPage,name);
            req.setAttribute("list",list);
            long ArtCount = articleService.getArticleCount(name);

            //总分页
            long pageTotal = PageUtil.PageTotal(ArtCount);
//            System.out.println(pageTotal);
            //起始页
            long startPage = PageUtil.getStartPage(currentPage);
//            System.out.println(startPage);
            //结束页
            long endPage = PageUtil.getEndPage(currentPage, ArtCount);
//            System.out.println(endPage);
            req.setAttribute("currentPage",currentPage);
            req.setAttribute("ArtCount",ArtCount);
            req.setAttribute("name",name);
            req.setAttribute("pageTotal",pageTotal);
            req.setAttribute("pageCount", StrMath.PAGE_COUNT);
            req.setAttribute("startPage",startPage);
            req.setAttribute("endPage",endPage);
            req.getRequestDispatcher("/html/article.jsp").forward(req,resp);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public void release(HttpServletRequest req, HttpServletResponse resp){
        try {
            String id = req.getParameter("id");
            Map<String, Object> mapList = articleService.getArticleList(id);
            if (null != mapList){
                req.getSession().setAttribute("mapList",mapList);
                resp.sendRedirect("/html/article_detail.jsp");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void articleAdd(HttpServletRequest req, HttpServletResponse resp){
        try {
            String title = req.getParameter("title");
            String content = req.getParameter("content");
            String userId = getUserId(req);
            String userName = getUserName(req);
            if (null != title && null != content){
                Article article = new Article();
                article.setTitle(title);
                article.setContent(content);
                article.setPublishDate(new Date());
                article.setPublishUsername(userName);
                article.setUserId(Integer.parseInt(userId));
                boolean b = articleService.AddArticle(article);
                if (b){
                    //发布成功
                    req.getSession().setAttribute("ErrorMsg","发布文章成功");
                    resp.sendRedirect("/article/lookRelease");

                }else {
                    //发布失败
                    req.getSession().setAttribute("ErrorMsg","发布文章失败");
                    resp.sendRedirect("/article/lookRelease");

                }
            }
//            req.getRequestDispatcher("/html/article.jsp").forward(req,resp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void collection(HttpServletRequest req, HttpServletResponse resp){
        try {
            String page = req.getParameter("page");
            String name = req.getParameter("name");
            String userId = getUserId(req);
            int currentPage = 1;
            if (null != page){
                currentPage = Integer.parseInt(page);
            }
            List<Map<String,Object>> list = articleService.getCollectionArticle(currentPage,userId,name);
            req.getSession().setAttribute("list",list);
            long CollectionCount = articleService.getCollectionCount(userId,name);
            //总分页
            long pageTotal = PageUtil.PageTotal(CollectionCount);
            //起始页
            long startPage = PageUtil.getStartPage(currentPage);
            //结束页
            long endPage = PageUtil.getEndPage(currentPage, CollectionCount);
            req.setAttribute("currentPage",currentPage);
            req.setAttribute("CollectionCount",CollectionCount);
            req.setAttribute("name",name);
            req.setAttribute("pageTotal",pageTotal);
            req.setAttribute("pageCount", StrMath.PAGE_COUNT);
            req.setAttribute("startPage",startPage);
            req.setAttribute("endPage",endPage);
            req.getRequestDispatcher("/html/article_collect.jsp").forward(req,resp);



        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public void AddCollection(HttpServletRequest req, HttpServletResponse resp){
        try {
            //文章id
            String id = req.getParameter("id");
//            System.out.println("文章id"+id);
            //收藏用户id
            String uId = req.getParameter("uId");
//            System.out.println("收藏用户id"+uId);
            //登录用户id
            String userId = getUserId(req);
//            System.out.println("登录用户id"+userId);
            Favorite favorite = articleService.ArtucileCollection(userId,id);
            if (null != favorite){
                resp.getWriter().write("已经收藏了");
            }else {
                boolean b = articleService.CollectionArttcile(userId,id);
                if (b){
                    resp.getWriter().write("1");
                    System.out.println("收藏成功1");
                }else {
                    resp.getWriter().write("添加收藏失败");
                    System.out.println("添加收藏失败");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }


    }

    public void DeleteCollection(HttpServletRequest req, HttpServletResponse resp){
        try {
            //文章id
            String id = req.getParameter("id");
//            System.out.println("文章id"+id);
            //收藏用户id
            String uId = req.getParameter("uId");
//            System.out.println("收藏用户id"+uId);
            //登录用户id
            String userId = getUserId(req);
            Favorite favorite = articleService.ArtucileCollection(userId, id);
            if (null != favorite){
                boolean b = articleService.DeleteConllctrionArticle(favorite.getId());
                if (b){
                    resp.getWriter().write("1");
                    System.out.println("取消收藏成功");
                }else {
                    resp.getWriter().write("取消收藏失败");
                    System.out.println("取消收藏失败");
                }
            }else {
                resp.getWriter().write("收藏成功");
                System.out.println("收藏成功2");
            }

        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}
